﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using BLL;
using System.Reflection;
using System.Data.Common;
using Microsoft.Reporting.WinForms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace QLTB_CTK34
{
    public partial class FrmInAn : Form
    {
        DbQuanLiTBDataContext dc;
        private DKForm DieuKien;
        public FrmInAn()
        {
            InitializeComponent();
        }

        public FrmInAn(DKForm _dk)
            : this()
        {
            DieuKien = _dk;
            dc = new DbQuanLiTBDataContext(MyConnectString.GetMyConnectString);
        }
        #region đôi ienumerable sang datatable
        public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
        {
            DataTable dtReturn = new DataTable();
            PropertyInfo[] oProps = null;
            if (varlist == null) return dtReturn;
            foreach (T rec in varlist)
            {
                if (oProps == null)
                {
                    oProps = ((Type)rec.GetType()).GetProperties();
                    foreach (PropertyInfo pi in oProps)
                    {
                        Type colType = pi.PropertyType;
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
                        == typeof(Nullable<>)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }
                        dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                    }
                }
                DataRow dr = dtReturn.NewRow();
                foreach (PropertyInfo pi in oProps)
                {
                    dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                    (rec, null);
                }
                dtReturn.Rows.Add(dr);
            }
            return dtReturn;
        }
        #endregion

        private void FormInAn_Load(object sender, EventArgs e)
        {
            DKLoad(DieuKien);
        }
        DataTable tb;
        /// <summary>
        /// Tim Kiem Dữ liệu Để In Ấn 
        /// </summary>
        private void DKLoad(DKForm dk)
        {
            reportViewer1.LocalReport.DataSources.Clear();
            
            switch (dk)
            {
                
                case DKForm.thietbi:
                    tb = new Reporting.ThietBiDataTable();
                    var querythietbi = from thietbi in dc.ThietBis
                                select thietbi;
                    querythietbi.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                    reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.ThietBi.rdlc";
                    this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("ThietBi",(DataTable)tb));
                    reportViewer1.LocalReport.Refresh();
                    
                    break;
                case DKForm.KQBaoTri:
                    tb = new Reporting.KetQuaBaoTriDataTable();
                    var querybt = from baotri in dc.KQBaoTriTBs
                                select baotri;
                    querybt.ToList().ForEach(x=>tb.Rows.Add(x.MaThongBao,x.MaNguoiGui,x.MaTB,x.TinhTrangLoi,x.NgayBaoTri,x.KetQua));
                    reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.KQBaoTri.rdlc";
                    this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("KetQuaBT",(DataTable)tb));
                    reportViewer1.LocalReport.Refresh();
                    break;
                case DKForm.taikhoan:
                    tb = new Reporting.TaiKhoanDataTable();
                    var query = from taikhoan in dc.TaiKhoans
                                select taikhoan;
                    query.ToList().ForEach(x=>tb.Rows.Add(x.MaTK,x.TenDangNhap,x.LanCuoiDangNhap,x.MayDangNhap,x.TrangThai));
                    reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.TaiKhoan.rdlc";
                    this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("TaiKhoan",(DataTable)tb));
                    reportViewer1.LocalReport.Refresh();

                    btnTTTK.Visible = true;
                    break;
                case DKForm.TBLoiMT:
                    tb = new Reporting.TBLoiMTDataTable();
                    var querymt = from loimt in dc.ThongBaoLoiMayTinhs
                                select loimt;
                    querymt.ToList().ForEach(x=>tb.Rows.Add(x.MaLoiMT,x.MaLoiTB,x.TenPhanBiLoi,x.ChiTietLoi));
                    reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.TBLoiMT.rdlc";
                    this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("TBLoiMT",(DataTable)tb));
                    reportViewer1.LocalReport.Refresh();
                    break;
                case DKForm.TBLoiTB:
                   tb = new Reporting.TBLoiTBDataTable();
                    var querytb = from loitb in dc.ThongBaoLoiThietBis
                                select loitb;
                    querytb.ToList().ForEach(x=>tb.Rows.Add(x.MaLoiTB,x.MaNguoiGui,x.MaTB,x.LoaiTB,x.NoiDungLoi,x.NgayGui,x.GhiChu));
                    reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.TBLoiTB.rdlc";
                    this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1",(DataTable)tb));
                    reportViewer1.LocalReport.Refresh();
                    break;
                case DKForm.DeXuat:
                    tb = new Reporting.DeXuatDataTable();
                     var querydx = from dexuat in dc.DeXuats
                                select dexuat;
                    querydx.ToList().ForEach(x=>tb.Rows.Add(x.MaDX,x.MaNguoiGui,x.NoiDung));
                    reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.DeXuat.rdlc";
                    this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DeXuat",(DataTable)tb));
                    reportViewer1.LocalReport.Refresh();
                    break;
                default:
                    tb = new DataTable();
                    break;
            }
            this.reportViewer1.RefreshReport();
            for (int i = 0; i < tb.Columns.Count; i++)
                cbbDK.Items.Add(tb.Columns[i].ColumnName);
        }

        private void rpvInAn_Load(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// Khi click vào button thông tin cá nhân hiện dữ liệu thông tin tài khoản và ngược lại hiện danh sách tài khoản
        /// </summary>
        private int click = 0;
        private void btnTTTK_Click(object sender, EventArgs e)
        {
            cbbDK.Items.Clear();
            if (click == 0)
            {
                btnTTTK.Text = "Danh Sách Tài Khoản";
                reportViewer1.LocalReport.DataSources.Clear();
                Reporting.ThongTinTaiKhoanDataTable tb = new Reporting.ThongTinTaiKhoanDataTable();
                var query = from TTTK in dc.ThongTinNguoiDungs
                            select TTTK;
                query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.Ho, x.Ten, x.NgaySinh, x.CMND,x.SDT,x.ChucVu));
                reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.ThongTinTK.rdlc";
                this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("ThonTinTK", (DataTable)tb));
                reportViewer1.LocalReport.Refresh();
                this.reportViewer1.RefreshReport();
                click++;
                for (int i = 0; i < tb.Columns.Count; i++)
                    cbbDK.Items.Add(tb.Columns[i].ColumnName);
            }
            else if (click == 1)
            {
                btnTTTK.Text = "Thông Tin Tài Khoản";
                DKLoad(DKForm.taikhoan);
                click--;
            }
        }

        private void reportViewer1_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// Khi click vào button lọc dữ liệu Tùy theo Dk Sẽ Lọc Dữ Liệu Để In Ấn.
        /// </summary>
        private void btnLoc_Click(object sender, EventArgs e)
        {
            LocData(); 
        }

        private void LocData()
        {
            ThuVien.TRY_CATCH(()=>
            {
                reportViewer1.LocalReport.DataSources.Clear();
                switch (DieuKien)
                {
                    case DKForm.thietbi:
                        tb = ThuVien.LocData((thietbi)cbbDK.SelectedIndex, txtDK.Text);
                        reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.ThietBi.rdlc";
                        this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("ThietBi", (DataTable)tb));
                        reportViewer1.LocalReport.Refresh();
                        break;
                    case DKForm.KQBaoTri:
                        tb = ThuVien.LocData((ketquabaotri)cbbDK.SelectedIndex, txtDK.Text);
                        reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.KQBaoTri.rdlc";
                        this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("KetQuaBT", (DataTable)tb));
                        reportViewer1.LocalReport.Refresh();
                        break;
                    case DKForm.taikhoan:
                        tb = ThuVien.LocData((taikhoan)cbbDK.SelectedIndex, txtDK.Text);
                        reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.TaiKhoan.rdlc";
                        this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("TaiKhoan", (DataTable)tb));
                        reportViewer1.LocalReport.Refresh();
                        break;
                    case DKForm.TBLoiMT:
                        tb = ThuVien.LocData((thongbaoloimt)cbbDK.SelectedIndex, txtDK.Text);
                        reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.TBLoiMT.rdlc";
                        this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("TBLoiMT", (DataTable)tb));
                        reportViewer1.LocalReport.Refresh();
                        break;
                    case DKForm.TBLoiTB:
                        tb = ThuVien.LocData((thongbaoloitb)cbbDK.SelectedIndex, txtDK.Text);
                        reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.TBLoiTB.rdlc";
                        this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", (DataTable)tb));
                        reportViewer1.LocalReport.Refresh();
                        break;
                    case DKForm.DeXuat:
                        tb = ThuVien.LocData((dexuat)cbbDK.SelectedIndex, txtDK.Text);
                        reportViewer1.LocalReport.ReportEmbeddedResource = "QLTB_CTK34.Reporting.DeXuat.rdlc";
                        this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DeXuat", (DataTable)tb));
                        reportViewer1.LocalReport.Refresh();
                        break;
                    default:
                        break;
                }
                this.reportViewer1.RefreshReport();
            }
            );
        }
    }
}
